package com.itqf.Controller;
/**
 * 大帅 2019/10/14 21:21
 */

import com.github.pagehelper.PageInfo;

import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Answer.AnswerService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.UpLoadUtil;
import com.itqf.Util.myPageInfo;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * author: 刁晨西
 * date: 2019/10/14 21:21
 * description:
 */
@Controller
public class AnswerController {

    @Resource
    private AnswerService answerService;


/** 
* @Description: 根据问题的id 查询全部的答案
* @Param: [pageindex, anid]
* @return: com.itqf.entity.Manager 
* @Author: 陈岗阳
* @Date: 2019/10/20 
*/ 
    @RequestMapping(method = RequestMethod.GET,value = "/huileme/a/m/AnswerController/detailsProblem")
    @ResponseBody
    public ResultDTO<PageInfo> findalla(@RequestParam(defaultValue = "1") int pageindex,@RequestParam(defaultValue = "-1") int requsetionid){

        ResultDTO resultDTO=new ResultDTO();

        PageInfo info = answerService.findall(pageindex, myPageInfo.PAGESIZE,requsetionid);
        if (!info.getList().isEmpty())
        {
            resultDTO.setData(info);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(info);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }

        return resultDTO;
    }

/** 
* @Description: 答案的增加图片的上传
* @Param: [answerQuestions 问题的图片 , request, answerTitle]
* @return: com.itqf.entity.Manager 
* @Author: 陈岗阳
* @Date: 2019/10/20 
*/ 
    @RequestMapping(method = RequestMethod.POST,value = "/huileme/a/u/AnswerController/inserta")
    @ResponseBody
    public ResultDTO inserta( MultipartFile answerQuestions, HttpServletRequest request,String answerTitle,int requestionid){

       ResultDTO resultDTO=new ResultDTO();

        if(answerQuestions==null){
            resultDTO.setMessage(CodeEnum.parameterincomplete.getMsg());
            resultDTO.setCode(CodeEnum.parameterincomplete.getCode());
            return resultDTO;
        }
        //将文件上传到upload目录下
        String originalFilename = UpLoadUtil.getup(answerQuestions, request);

//        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

//        String userid="1";

       int i =answerService.insertAnserer(originalFilename,userid1,answerTitle,requestionid);

        if (i<0){
            //没有查询到 返回200状态码

            resultDTO.setMessage(CodeEnum.adderror.getMsg());
            resultDTO.setCode(CodeEnum.adderror.getCode());
            return resultDTO;
        }else
        {
            resultDTO.setMessage(CodeEnum.addsuccess.getMsg());
            resultDTO.setCode(CodeEnum.addsuccess.getCode());

        }


        return resultDTO;
    }

/** 
* @Description:  根据用户id删除问题
* @Param: [answeruserid, requestionId] 
* @return: com.itqf.entity.Manager 
* @Author: 陈岗阳
* @Date: 2019/10/20 
*/
    @RequestMapping(method = RequestMethod.DELETE,value ="/huileme/a/u/AnswerController/deletea")
    @ResponseBody
    public ResultDTO deletea(@RequestParam(defaultValue = "-1")int  ansrequestionId,@RequestParam(defaultValue = "-1") int requestionId,HttpServletRequest request){

        ResultDTO resultDTO=new ResultDTO();


        if (requestionId==-1&&ansrequestionId==-1)
        {
           resultDTO.setMessage(CodeEnum.parameterincomplete.getMsg());
           resultDTO.setCode(CodeEnum.parameterincomplete.getCode());
            return resultDTO;
        }
        //        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

        int i = answerService.deleteByid(ansrequestionId, requestionId, userid1);
        if (i<0)
        {
            resultDTO.setMessage(CodeEnum.deleteerror.getMsg());
            resultDTO.setCode(CodeEnum.deleteerror.getCode());
        }
        if(i>0)
        {
            resultDTO.setMessage(CodeEnum.deletesuccess.getMsg());
            resultDTO.setCode(CodeEnum.deletesuccess.getCode());
        }

        return resultDTO;
    }

    /** 
    * @Description:  根据用户id和回答id 修改
    * @Param: [answerQuestions, request, answerTitle, ansrequestionId]
    * @return: com.itqf.entity.Manager 
    * @Author: 陈岗阳
    * @Date: 2019/10/20    400
    */ 
    @RequestMapping(method = RequestMethod.POST,value ="/huileme/a/u/AnswerController/updatea")
    @ResponseBody
    public ResultDTO updatea( MultipartFile answerQuestions, HttpServletRequest request,String answerTitle,int ansrequestionId){

        ResultDTO resultDTO=new ResultDTO();


        if(ansrequestionId==-1)
        {
            resultDTO.setMessage(CodeEnum.Updateerror.getMsg());
            resultDTO.setCode(CodeEnum.Updateerror.getCode());
            return resultDTO;
        }
        if(answerQuestions==null){
           resultDTO.setMessage(CodeEnum.parameterincomplete.getMsg());
            resultDTO.setCode(CodeEnum.parameterincomplete.getCode());
            return resultDTO;
        }
        //将文件上传到upload目录下
        String originalFilename = UpLoadUtil.getup(answerQuestions, request);


//                调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

//        String userid="1";

        int i =answerService.updateAnserer(originalFilename,userid1,answerTitle,ansrequestionId);

        if (i<0){
            resultDTO.setMessage(CodeEnum.Updateerror.getMsg());
            resultDTO.setCode(CodeEnum.Updateerror.getCode());
            return resultDTO;
        }

        resultDTO.setMessage(CodeEnum.Updatesuccess.getMsg());
        resultDTO.setCode(CodeEnum.Updatesuccess.getCode());
        return resultDTO;
    }

}
